package com.hyperfix.washer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyperfix.models.washer.dtos.PipelineDTO;
import com.hyperfix.models.washer.dtos.PipelineT;
import com.hyperfix.models.washer.pojos.Pipeline;
import com.hyperfix.models.washer.vos.PipelineQueryVO;
import com.hyperfix.models.washer.vos.PipelineVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author JiangChunYu
 * 2024/4/14
 */
@Mapper
public interface PipelineMapper extends BaseMapper<Pipeline> {
    IPage<PipelineDTO> listPipelineDTO(IPage<PipelineDTO> pipelineDTOIPage, @Param("params") PipelineQueryVO pipelineQueryVO);

    @Select("select * from pipeline where id = (select pipeline_id from monitor where user_id = #{userId})")
    Pipeline selectOneByUserId(@Param("userId") Integer userId);

    @Select("select id from pipeline where name = #{pipelineName}")
    Integer selectIdByName(@Param("pipelineName") String pipelineName);

    @Select("select job_id from schedule_setting where pipeline = #{pipelineId}")
    Integer selectJobId(@Param("pipelineId") Integer pipelineId);

    @Select("select p.id, p.name, ss.job_status as status from pipeline p left join schedule_setting ss on p.id = ss.pipeline order by p.id")
    List<PipelineT> selectAllWithStatus();

    @Select("select count(*) as pipelineNum from pipeline p left join schedule_setting ss on p.id = ss.pipeline where ss.job_status = 1")
    Integer selectPipelineNum();

    @Select("select id, name from pipeline order by id")
    List<PipelineT> selectPipelineIdName();

    @Update("update pipeline set name = #{name}, type = #{type}, length = #{length}, radius = #{radius} where id = #{id}")
    boolean updatePipeline(PipelineVO pipelineVO);

    @Select("select id from pipeline")
    List<Integer> selectIdList();

    @Select("select p.* from pipeline p left join station s on p.station_id = s.id where s.team_id = #{teamId} order by p.id")
    List<Pipeline> selectAllByTeamId(@Param("teamId") Integer teamId);
}